<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>scrollToTop</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body,
        html {
            height: 100%;
        }

        .scroll-container {
            height: 100%;
        }

        .page {
            height: 100%;
            width: 100%;
            color: #fff;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 4em;
            letter-spacing: 2px;
        }

        .page-1 {
            background-color: #2396ef;
        }

        .page-2 {
            background-color: #3bef23;
        }

        .page-3 {
            background-color: #ef23c3;
        }

        .page-4 {
            background-color: #efca23;
        }

        .to-top {
            font-size: 2rem;
            color: #2396ef;
            width: 35px;
            height: 35px;
            position: fixed;
            bottom: 15px;
            right: 15px;
            transition: all .3s cubic-bezier(0.075, 0.82, 0.165, 1);
            border-radius: 50%;
            background-color: #1f252b;
            text-align: center;
            cursor: pointer;
        }

        .to-top:hover {
            background-color: #010e1b;
            color: #3ca3f1;
            box-shadow: 0 0 13px rgba(0, 0, 0, .5);
        }

        ::-webkit-scrollbar {
            width: 5px;
            height: 5px;
            background-color: #f1f1f1;
        }

        ::-webkit-scrollbar-thumb {
            width: 5px;
            height: 5px;
            background-color: #4651e6;
        }
    </style>
</head>

<body>
    <div class="scroll-container">
        <section class="page-1 page">页面一</section>
        <section class="page-2 page">页面二</section>
        <section class="page-3 page">页面三</section>
        <section class="page-4 page">页面四</section>
    </div>
    <div class="to-top">^</div>
    <script>
        const scrollContainer = document.querySelector('.scroll-container');
        const toTop = document.querySelector('.to-top');
        const scrollToTop = () => {
            let c = document.documentElement.scrollTop || document.body.scrollTop;
            if (c > 0) {
                window.requestAnimationFrame(scrollToTop);
                window.scrollTo(0, c - c / 8);
            }
        }
        toTop.addEventListener('click', () => scrollToTop());
    </script>
</body>

</html>